<template>
  <div class="brand">
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="">
        <el-input
          v-model="formInline.id"
          placeholder="请输入品牌商ID"
        />
      </el-form-item>
      <el-form-item label="">
        <el-input
          v-model="formInline.name"
          placeholder="请输入品牌商名称"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          @click="handleLsh"
        >查询</el-button>
        <el-button
          type="primary"
          icon="el-icon-edit"
          @click="handleAdd(1)"
        >添加</el-button>
        <el-button
          type="primary"
          icon="el-icon-download"
          @click="handleDaochu"
        >导出</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="品牌商id" />
      <el-table-column prop="name" label="品牌商名称" />
      <el-table-column prop="picUrl" label="品牌商图片">
        <template slot-scope="scope">
          <img :src="scope.row.picUrl" width="60px" alt="">
        </template>
      </el-table-column>
      <el-table-column prop="desc" label="介绍" width="800px" />
      <el-table-column prop="floorPrice" label="底价" />
      <el-table-column prop="" label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            @click="handleDet(scope.row, 2)"
          >编辑</el-button>
          <el-button
            type="danger"
            size="mini"
            @click="handleDel(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="obj.currentPage"
      :page-sizes="[10, 20, 30, 40, 50, 100]"
      :page-size="obj.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="obj.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!-- 模态框 -->
    <el-dialog
      :visible.sync="dialogVisible"
      width="40%"
      :before-close="handleClose"
    >
      <p v-if="obj.val == '1'">创建</p>
      <p v-else-if="obj.val == '2'">编辑</p>
      <el-form
        ref="ruleForm"
        :model="ruleForm"
        :rules="rules"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="品牌商名称" prop="name">
          <el-input v-model="ruleForm.name" />
        </el-form-item>
        <el-form-item label="介绍">
          <el-input v-model="ruleForm.desc" />
        </el-form-item>
        <el-form-item label="品牌商图片">
          <el-upload
            v-model="ruleForm.picUrl"
            action="http://admin.bsmall.byesame.com/admin/storage/create"
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :on-success="handleAvatarSuccess"
            :on-remove="handleRemove"
          >
            <img
              v-if="ruleForm.picUrl"
              width="100%"
              :src="ruleForm.picUrl"
              :v-model="ruleForm.picUrl"
            >
            <i v-if="ruleForm.picUrl == ''" class="el-icon-plus" />
          </el-upload>
          <!-- <el-dialog :visible.sync="dialogVisibles">
            <img
              width="100%"
              v-if="ruleForm.picUrl"
              :src="ruleForm.picUrl"
              alt=""
            /> -->
          <!-- </el-dialog> -->
        </el-form-item>
        <el-form-item label="低价">
          <el-input v-model="ruleForm.floorPrice" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button
          v-if="obj.val == '1'"
          type="primary"
          @click="submitForm('ruleForm')"
        >确 定</el-button>
        <el-button
          v-else-if="obj.val == '2'"
          type="primary"
          @click="handleED"
        >保 存</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
// 导入插件
import * as XLSX from 'xlsx'
import { saveAs } from 'file-saver'
import { listBrand, deleteBrand, createBrand } from '@/api/brand'
import { createStorage } from '@/api/storage'
export default {
  data() {
    return {
      // 上传
      dialogVisibles: false,
      dialogVisible: false,
      imageUrl: '',
      obj: {
        currentPage: 1,
        pageSize: 20,
        total: null,
        val: null
      },
      na: '',
      formInline: {
        id: '',
        name: ''
      },
      tableData: [],
      ruleForm: {
        name: '',
        desc: '',
        picUrl: '',
        floorPrice: ''
      },
      rules: {
        name: [{ required: true, message: '请输入活动名称', trigger: 'blur' }]
      }
    }
  },
  mounted() {
    this.handleLsh()
  },
  methods: {
    // 调接口
    handleLsh() {
      const query = {
        page: this.obj.currentPage,
        limit: this.obj.pageSize,
        sort: 'add_time',
        order: 'desc',
        id: this.formInline.id,
        name: this.formInline.name
      }
      listBrand(query).then((response) => {
        console.log(response, '1212')
        this.tableData = response.data.data.list
        this.obj.total = response.data.data.total
      })
    },
    // 添加
    handleAdd(val) {
      this.dialogVisible = true
      this.obj.val = val;
      (this.ruleForm.name = '')(this.ruleForm.desc = '')(this.ruleForm.picUrl = '')(this.ruleForm.floorPrice = '')
    },
    // 模态框确定按钮
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.dialogVisible = false
          const data = {
            name: this.ruleForm.name,
            desc: this.ruleForm.desc,
            picUrl: this.ruleForm.picUrl,
            floorPrice: this.ruleForm.floorPrice
          }
          createBrand(data).then((response) => {
            console.log(response, '12125')
            this.$message({
              type: 'success',
              message: '添加成功!'
            })
            this.handleLsh();
            (this.ruleForm.name = '')(this.ruleForm.desc = '')(this.ruleForm.picUrl = '')(this.ruleForm.floorPrice = '')
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 导出
    handleDaochu() {
      // 创建一个workbook
      const workbook = XLSX.utils.book_new() // 导出数据
      const worksheet = XLSX.utils.json_to_sheet(this.tableData)
      XLSX.utils.book_append_sheet(workbook, worksheet, 'Sheet 1') // 将workbook转为二进制数据

      const excelData = XLSX.write(workbook, {
        type: 'array',
        bookType: 'xlsx'
      }) // 创建blob对象并保存excel文件
      const blob = new Blob([excelData], { type: 'application/octet-stream' }) // 设置导出文件名字
      saveAs(blob, 'data.xlsx')
    },
    // 编辑
    handleDet(row, val) {
      console.log(row, '编辑啊')
      this.dialogVisible = true
      this.obj.val = val;
      (this.ruleForm.name = row.name)(this.ruleForm.desc = row.desc)(this.ruleForm.picUrl = row.picUrl)(this.ruleForm.floorPrice = row.floorPrice)
      this.na = row
    },
    // 编辑确定按钮
    // handleDE() {
    //   let data = this.na;
    //   updateBrand(data).then((response) => {
    //     this.$message({
    //           type: "success",
    //           message: "添加成功!",
    //         });
    //   });
    // },
    // 上传头像
    handleRemove(file, fileList) {
      console.log(file, fileList)
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    handleAvatarSuccess(res, file) {
      createStorage().then((response) => {
      })
      this.imageUrl = URL.createObjectURL(file.raw)
    },
    // 删除
    handleDel(row) {
      console.log(row)

      deleteBrand(row).then((response) => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.handleLsh()
      })
    },
    // 分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.obj.pageSize = val
      this.handleLsh()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.obj.currentPage = val
      this.handleLSh()
    }
  }
}
</script>
<style lang="scss" scoped>
.brand ::v-deep {
  margin: 20px;
  .el-table {
    .el-table__cell {
      text-align: center;
    }
  }
  .el-pagination {
    margin: 40px 10px;
  }
  .el-dialog {
    p {
      position: absolute;
      top: 10px;
      left: 20px;
      font-size: 16px;
      font-weight: 600;
      color: #000;
    }
  }
}
</style>
